import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/evaluation.dart';
import 'package:ichinesereader/models/evaluation/evaluationOverviewResult.dart';
import 'package:ichinesereader/models/report/evaluationLevelChartData.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/charts/evaluationAccuracy.dart';
import 'package:ichinesereader/widgets/charts/evaluationLevelAverageStackedLines.dart';
import 'package:dropdown_search/dropdown_search.dart';
import '../../themes/appTheme.dart';

class BenchmarkOverviewContent extends StatefulWidget {
  BenchmarkOverviewContent(
      {Key? key,
      required this.levelStandard,
      required this.selectedStudentLists})
      : super(key: key);
  final String levelStandard;
  final List<StudentList> selectedStudentLists;
  _BenchmarkOverviewContentState createState() =>
      _BenchmarkOverviewContentState();
}

class _BenchmarkOverviewContentState extends State<BenchmarkOverviewContent> {
  final box = Hive.box('appconfig');
  final myKey = GlobalKey<DropdownSearchState<StudentList>>();
  final EvaluationController controller = Get.find();

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher evaluation benchmark overview content');

    return controller.obx(
      (data) {
        List<EvaluationLevelChart> chartData = [];
        if (this.widget.levelStandard == 'ICR' &&
            controller.benchmarkOverviewData.value.icrLevel != null) {
          controller.benchmarkOverviewData.value.icrLevel?.forEach((item) {
            EvaluationLevelChart chart = EvaluationLevelChart(
                item.level.toString(),
                item.listeningAccuracy! / 10,
                item.speakingScore?.toDouble(),
                null,
                item.readingAccuracy! / 10,
                item.writingScore?.toDouble(),
                0);
            chartData.add(chart);
          });
        }
        if (this.widget.levelStandard == 'ACTFL' &&
            controller.benchmarkOverviewData.value.actflLevel != null) {
          controller.benchmarkOverviewData.value.actflLevel?.forEach((item) {
            EvaluationLevelChart chart = EvaluationLevelChart(
                item.level.toString(),
                item.listeningAccuracy! / 10,
                item.speakingScore?.toDouble(),
                null,
                item.readingAccuracy! / 10,
                item.writingScore?.toDouble(),
                0);
            chartData.add(chart);
          });
        }
        return Column(children: [
          Container(
            margin: EdgeInsets.symmetric(vertical: 9.0),
            padding: EdgeInsets.symmetric(horizontal: 21.0),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(3.0),
              color: Get.isDarkMode
                  ? AppTheme.colors.darkTheme2.withOpacity(0.6)
                  : Colors.white.withOpacity(0.3),
            ),
            child: EvaluationLevelStackedLines(
              levelChartData: chartData,
              chartTitle: 'Class Average'.tr,
              xLabel: 'Score'.tr,
              yLabel: '',
              levelStandard: this.widget.levelStandard,
              mode: 'benchmark',
            ),
          ),
          Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3.0),
                color: Get.isDarkMode
                    ? AppTheme.colors.darkTheme2.withOpacity(0.6)
                    : Colors.white.withOpacity(0.3),
              ),
              child: EvaluationStackedLineChart(
                studentLists: this.widget.selectedStudentLists,
                levelChartData: chartData,
                chartTitle: 'Listening',
                xLabel: 'Accuracy'.tr,
                yLabel: '',
                levelStandard: this.widget.levelStandard,
              )),
          Container(
              margin: EdgeInsets.symmetric(vertical: 9.0),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3.0),
                color: Get.isDarkMode
                    ? AppTheme.colors.darkTheme2.withOpacity(0.6)
                    : Colors.white.withOpacity(0.3),
              ),
              child: EvaluationStackedLineChart(
                studentLists: this.widget.selectedStudentLists,
                levelChartData: chartData,
                chartTitle: 'Reading',
                xLabel: 'Accuracy'.tr,
                yLabel: '',
                levelStandard: this.widget.levelStandard,
              )),
          Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3.0),
                color: Get.isDarkMode
                    ? AppTheme.colors.darkTheme2.withOpacity(0.6)
                    : Colors.white.withOpacity(0.3),
              ),
              child: EvaluationStackedLineChart(
                studentLists: this.widget.selectedStudentLists,
                levelChartData: chartData,
                chartTitle: 'Speaking',
                xLabel: 'Score'.tr,
                yLabel: '',
                levelStandard: this.widget.levelStandard,
              )),
          Container(
              margin: EdgeInsets.symmetric(vertical: 9.0),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(3.0),
                color: Get.isDarkMode
                    ? AppTheme.colors.darkTheme2.withOpacity(0.6)
                    : Colors.white.withOpacity(0.3),
              ),
              child: EvaluationStackedLineChart(
                studentLists: this.widget.selectedStudentLists,
                levelChartData: chartData,
                chartTitle: 'Writing',
                xLabel: 'Score'.tr,
                yLabel: '',
                levelStandard: this.widget.levelStandard,
              )),
        ]);
      },
      onEmpty: Text('Data not available ...'.tr),
      //onLoading: Center(child: CircularProgressIndicator()),
    );
  }
}
